package com.cldz.controller;

import com.cldz.common.dto.req.DevControlDto;
import com.cldz.common.dto.res.DeviceControlResDto;
import com.cldz.feign.DevControlFeign;
import com.cldz.service.DevContolService;
import com.cldz.response.support.ServerResponse;
import com.cldz.dao.UserPropertyMapper;
import com.cldz.utils.MathUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 14:50 2018/12/17
 * @ Description：
 * @ Modified By：
 */
@Slf4j
@RestController
public class DevControlController implements DevControlFeign {

    @Autowired
    private DevContolService devContolService;

    @Autowired
    private UserPropertyMapper userPropertyMapper;

    /**
     * 获取指定设备控制记录
     * @param devControlDto
     * @return
     */
    @GetMapping("/devControlRecord")
    public ServerResponse getControlRecordByDeviceID(DevControlDto devControlDto){
        List<DeviceControlResDto> list = devContolService.getControlRecordByDeviceID(devControlDto);
        Integer amount = devContolService.getControlRecordAmountByDeviceID(devControlDto);
        return ServerResponse.createDataAndCount(amount,list);
    }

    /**
     * 获取全部设备控制记录
     * @param devControlDto
     * @return
     */
    @GetMapping("/allControlRecord")
    public ServerResponse getAllControlRecord(DevControlDto devControlDto){
        Integer areaID = devControlDto.getAreaID();

        if(areaID == null){
            //没有根据用户查询区域编号
            areaID = userPropertyMapper.queryAreaIDByUserID(devControlDto.getUserID());
        }
        devControlDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));

        List<DeviceControlResDto> list = devContolService.getAllControlRecord(devControlDto);
        Integer amount = devContolService.getAllControlRecordAmount(devControlDto);
        return ServerResponse.createDataAndCount(amount,list);
    }
}
